package com.example.news.ui;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.OrientationHelper;
import androidx.recyclerview.widget.RecyclerView;

import com.example.news.Video.MyLayoutManager;
import com.example.news.R;
import com.example.news.Weather.Weather;
import com.example.news.Weather.WeatherAdapter;



import com.example.news.Weather.MojiDataManager;

import com.example.news.Weather.SixDayList;

import com.google.gson.Gson;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class WeatherFragment extends Fragment {

    private String date;
    private String temp;
    private String windDir;
    private String Fweather;
    private TextView Temp;
    private TextView weather;
    private TextView wind;

    private List<Weather> list = new ArrayList<>();
    public RecyclerView recyclerView;
    private WeatherAdapter weatherAdapter;
    private View view;
    OkHttpClient okHttpClient = new OkHttpClient();



    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.weatherfragment,container,false);
        iniRec();
        iniWeather();
        return view;
    }

    private void iniRec(){
        Temp = (TextView)view.findViewById(R.id.temp);
        weather = (TextView)view.findViewById(R.id.tv_main_weather);
        wind = (TextView)view.findViewById(R.id.tv_main_wind);
        recyclerView = view.findViewById(R.id.rv_main_sixDay);
        weatherAdapter = new WeatherAdapter(getActivity(),list);
        recyclerView.setAdapter(weatherAdapter);
        //recyclerView.setLayoutManager(new LinearLayoutManager(getActivity(),LinearLayoutManager.HORIZONTAL,false));
        recyclerView.setLayoutManager(new MyLayoutManager(getActivity(), OrientationHelper.HORIZONTAL,false));
        //recyclerView.addItemDecoration(new DividerItemDecoration(getActivity(),DividerItemDecoration.VERTICAL));

    }


    private void iniWeather(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                RequestBody requestBody = new FormBody.Builder()
                        .add("cityId", "917")
                        .add("token", MojiDataManager.BRIEFFORECAST6DAYSTOKEN)
                        .build();
                Request request = new Request.Builder()
                        .addHeader("Authorization", "APPCODE " + MojiDataManager.APPCODE)
                        .url(MojiDataManager.BRIEFFORECAST6DAYSURL)
                        .post(requestBody)
                        .build();
                Call call = okHttpClient.newCall(request);
                call.enqueue(new Callback() {
                    @Override
                    public void onFailure(@NotNull Call call, @NotNull IOException e) {

                    }

                    @Override
                    public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                        String result = response.body().string();
                        getActivity().runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Gson gson = new Gson();
                                SixDayList sixDay = gson.fromJson(result, SixDayList.class);
                                Temp.setText(sixDay.getData().getForecast().get(0).getTempDay()+"°");
                                weather.setText(sixDay.getData().getForecast().get(0).getConditionDay());
                                wind.setText(sixDay.getData().getForecast().get(0).getWindDirDay());
                                for (int i = 0; i < 6; i++){
                                    date = String.valueOf(sixDay.getData().getForecast().get(i).getPredictDate());
                                    temp = String.valueOf(sixDay.getData().getForecast().get(i).getTempDay());
                                    Fweather = String.valueOf(sixDay.getData().getForecast().get(i).getConditionDay());
                                    windDir = String.valueOf(sixDay.getData().getForecast().get(i).getWindDirDay());
                                    list.add(new Weather(date,temp,windDir,Fweather));
                                }
                                weatherAdapter.notifyDataSetChanged();
                            }
                        });
                    }
                });

            }
        }).start();

    }



    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        Log.e("TAG", "OneFragment onHiddenChanged :" + hidden);
    }
}